package com.runshu.newsmartark.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.runshu.newsmartark.R;
import com.runshu.newsmartark.activity.PhysicaInfoActivity;
import com.runshu.newsmartark.adapter.PhysicalManagerAdapter;
import com.runshu.newsmartark.base.BaseLazyFragmentPlus;
import com.runshu.newsmartark.entity.PhysicalEntity;
import com.runshu.newsmartark.mvp.contract.PhysicalContract;
import com.runshu.newsmartark.mvp.model.PhysicalModel;
import com.runshu.newsmartark.mvp.presenter.PhysicalPresenter;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.util.MacUtils;
import com.runshu.newsmartark.util.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/11/3
 * @decs：
 */

public class PhysicalManagerItemFragment extends BaseLazyFragmentPlus implements PhysicalContract.View{
    @BindView(R.id.recy_phy)
    RecyclerView mRecyPhy;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    private PhysicalManagerAdapter mAdapter;
    private List<PhysicalEntity.ContentBean> mData;
    private int pageSize = 20;
    private int pageNum = 1;
    private boolean loading = true;
    private boolean isLoadMore = false;
    private PhysicalPresenter physicalPresenter;
    private String deliveryStatus;
    @Override
    protected int getLayoutId() {
        return R.layout.frg_phy_manager;
    }

    @Override
    protected void initData() {
        mData = new ArrayList<>();
        mAdapter = new PhysicalManagerAdapter(mData);
        mRecyPhy.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mRecyPhy.setAdapter(mAdapter);
        mAdapter.bindToRecyclerView(mRecyPhy);
        mAdapter.setEmptyView(R.layout.empty_view);
    }

    @Override
    protected void initView(View layoutInflateView) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            deliveryStatus = bundle.getString("deliveryStatus");
        }
        physicalPresenter = new PhysicalPresenter(this,new PhysicalModel());
    }

    @Override
    protected void bindListener() {
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loading = false;
                isLoadMore = false;
                pageNum = 1;//设为默认加载第一页
                mData=new ArrayList<>();
                physicalPresenter.getDeliveryList(MacUtils.getFormatMacAddr(), pageNum,pageSize,deliveryStatus);

            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loading = false;
                isLoadMore = true;
                physicalPresenter.getDeliveryList(MacUtils.getFormatMacAddr(), pageNum,pageSize,deliveryStatus);
            }
        });
        mSwipeLayout.setRefreshing(true);
        mSwipeLayout.post(() -> {
            physicalPresenter.getDeliveryList( MacUtils.getFormatMacAddr(),pageNum,pageSize,deliveryStatus);
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent =  new Intent(context, PhysicaInfoActivity.class);
                intent.putExtra("phyID", mAdapter.getData().get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onGetDeliveryListSuccess(ResponseBean responseBean) {
        mSwipeLayout.setRefreshing(false);//完成刷新
        PhysicalEntity phyManagerEntity = (PhysicalEntity) responseBean.pullData();
        mData = phyManagerEntity.getRecords();
        if (isLoadMore) {
            mAdapter.loadMoreComplete();//网络请求成功的话调用loadMoreComplete()
        }
        if (mData != null && mData.size() > 0) {//有了请求数据
            if (isLoadMore) {//为true时，是加载更多获取的数据
                mAdapter.addData(mData);//加载更多的数据，add进去
            } else {
                mAdapter.setNewData(mData);//进入或者上拉加载的数据(page=1),setnewdata
            }
            if (mData.size() == pageSize) {//获取的数据未10条时，说明还有下一页，page加1，以备下次请求
                pageNum++;
                mAdapter.setEnableLoadMore(true);//允许加载更多
            } else {
                mAdapter.loadMoreEnd(true);//若这次获得的数据没有10条，说明全部加载完毕。
            }

        }else {
            if (pageNum == 1) {
                //数据得清空才会显示空布局
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
            }
            mAdapter.loadMoreEnd(true);//若这次获得的数据没有10条，说明全部加载完毕。

        }
    }

    @Override
    public void onError(String msg) {
        mSwipeLayout.setRefreshing(false);//完成刷新
        if (isLoadMore) {
            mAdapter.loadMoreFail();//请求失败的话调用loadMoreFail()
        }
        ToastUtils.showText(msg);
    }
}